package top.txl.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.txl.entity.ShoppingCart;
import top.txl.mapper.ShoppingCartMapper;
import top.txl.service.ShoppingCartService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartControllerImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public void addCart(ShoppingCart cart) {
        // 根据userId 去查，看数据库中是否存在 dishId相同的数据假如相同那么直接进行数量累加
        Integer userId = cart.getUserId();
        Integer dishId = cart.getDishId();
        Integer setmealId = cart.getSetmealId();

        ShoppingCart cartobj = new ShoppingCart();
        Integer number = null;
        Integer sId = null;
        Integer dId = null;

        if(dishId != null){
            cartobj = shoppingCartMapper.findCartListByDishId(userId, dishId);
            if(cartobj != null){
                number = cartobj.getNumber();
                dId = cartobj.getDishId();
            }

        }else if(setmealId != null){
            cartobj = shoppingCartMapper.findCartListBySetmealId(userId, setmealId);
            if(cartobj != null){
                sId = cartobj.getSetmealId();
                number = cartobj.getNumber();
            }
        }

        if(number != null){
            if(dId != null){
                shoppingCartMapper.updateCartListsNumber(userId,dId,number + 1);
            }else if(sId != null){
                shoppingCartMapper.updateCartListsNumberBySId(userId,sId,number + 1);
            }

        }else{
            cart.setCreateTime(LocalDateTime.now());
            cart.setNumber(1);
            if(cart.getSetmealId() == null){
                cart.setSetmealId(null);
            }
            if(cart.getDishFlavor() == null){
                cart.setDishFlavor(null);
            }
            if(cart.getDishId() == null){
                cart.setDishId(null);
            }
            shoppingCartMapper.addCart(cart);
        }
    }

    @Override
    public List<ShoppingCart> getCartLists(Integer userId) {
        List<ShoppingCart> lists = shoppingCartMapper.getCartLists(userId);
        log.info("lists,{}",lists);
        return lists;
    }

    @Override
    public void editCart(ShoppingCart cart) {
        // 判断当前是修改套餐，还是普通菜品
        Integer userId = cart.getUserId();
        Integer dishId = cart.getDishId();
        Integer sId = cart.getSetmealId();
        Integer number = cart.getNumber();

        if(dishId != null){
            if(number == 0){
                List<Integer> ids = new ArrayList<>();
                ids.add(dishId);
                shoppingCartMapper.updateCartListsDeletedDishId(userId,ids,1);
            }else{
                // 菜品
                shoppingCartMapper.updateCartListsNumber(userId,dishId,number);
            }
        }else if(sId != null){
            // 套餐
            if(number == 0){
                List<Integer> ids = new ArrayList<>();
                ids.add(sId);
                shoppingCartMapper.updateCartListsDeletedSetmealId(userId,ids,1);
            }else{
                shoppingCartMapper.updateCartListsNumberBySId(userId,sId,number);
            }
        }
    }

    @Override
    public void deletedAllCartByUserId(Integer userId) {
        shoppingCartMapper.deletedAllCartListsByUserId(userId);
    }
}
